package com.unknowpupil.web.test;

import com.github.pagehelper.PageHelper;
import com.unknowpupil.web.MxApplication;
import com.unknowpupil.web.domain.BlockHeight;
import com.unknowpupil.web.service.IBlockHeightService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import java.net.URL;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

/**
 * @Classname: MainTestApplication
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2019/10/8 14:58
 * @Description:
 * @Version: 1.0
 */
@RunWith(SpringRunner.class)
//这句就是让测试能够装载得到yml配置文件
@SpringBootTest(classes = MxApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//单元测试类中不允许有直接插入删除数据，必须要回滚。。。
@Transactional
@Rollback
public class MainTestApplication {

    @Autowired
    IBlockHeightService blockHeightService;

    //    内嵌servlet容器端口
    @LocalServerPort
    private int port;

    private URL base;
    /**
     * 这个就是测试Controller层的
     */
    @Autowired
    private TestRestTemplate restTemplate;

    @Before
    public void setUp()throws Exception{
        this.base = new URL("http://localhost:"+port+"/");
    }
    @Test
    public void contextLoads() {//断言
        ResponseEntity<String> respose = restTemplate.getForEntity(base.toString(),String.class);
        //这里的断言就是我返回回来的一定是Hello Spring Boot
        assertThat(((ResponseEntity) respose).getBody(),equalTo("Hello Spring Boot"));
    }


    @Test
    public void testAdd(){
        BlockHeight eth = blockHeightService.getCurrencyBolckHeight("");
        System.out.println(eth);
    }


    @Test
    public void page(){
        PageHelper.startPage(1,3);


    }

}
